package com.hyb.i18n.config;


import org.beetl.core.resource.ClasspathResourceLoader;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.beetl.ext.spring.BeetlSpringViewResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

import com.hyb.i18n.beetl.BeetlGroupConfiguration;

@Configuration
public class BeetlConfigure {

	@Value("${beetl.templatesPath}")
	String templatesPath;// 模板根目录 ，比如 "templates"
	
	@Value("${beetl.configPath}")
	String configPath;
	
	@Autowired
    private ResourceLoader resourceLoader;
	
	@Bean(initMethod = "init")
	public BeetlGroupUtilConfiguration beetlConfiguration() {
		BeetlGroupConfiguration beetlGroupUtilConfiguration = new BeetlGroupConfiguration();
		// 获取SpringBoot 的ClassLoader
		ClassLoader loader = Thread.currentThread().getContextClassLoader();
		if (loader == null) {
			loader = BeetlConfigure.class.getClassLoader();
		}
		Resource resource = resourceLoader.getResource(configPath);
		beetlGroupUtilConfiguration.setConfigFileResource(resource);
		ClasspathResourceLoader cploder = new ClasspathResourceLoader(loader, templatesPath);
		beetlGroupUtilConfiguration.setResourceLoader(cploder);
		return beetlGroupUtilConfiguration;

	}

	@Bean
	public BeetlSpringViewResolver beetlViewResolver() {
		BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
		beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
		beetlSpringViewResolver.setOrder(0);
		beetlSpringViewResolver.setConfig(beetlConfiguration());
		return beetlSpringViewResolver;
	}
}
